package com.pda.xinma.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;


import com.google.gson.Gson;
import com.pda.xinma.bean.ScanBean;


public class ScanButView extends androidx.appcompat.widget.AppCompatButton implements View.OnLongClickListener {

    private int soundid;
    private String barcodeStr;
    private boolean isScaning = false;
    private String mBroadcastAction ="nlscan.action.SCANNER_RESULT";
    private String mBroadcastLabel = "barcode";
    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            final String scanResult_1=intent.getStringExtra("SCAN_BARCODE1");
            final String scanResult_2=intent.getStringExtra("SCAN_BARCODE2");
            final int barcodeType = intent.getIntExtra("SCAN_BARCODE_TYPE", -1); // -1:unknown
            final String scanStatus=intent.getStringExtra("SCAN_STATE");

            if (action.equals(mBroadcastAction)) {
                if("ok".equals(scanStatus)){
                    //成功
                    barcodeStr = scanResult_1;
                    // TODO Auto-generated method stub
                    isScaning = false;
                    ScanBean scanBean = null;
                    try {
                        scanBean = new Gson().fromJson(barcodeStr, ScanBean.class);
                        if (scanBean.getType() == 0){
                            scanBean=new ScanBean(barcodeStr,0);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        //Toast.makeText(context, "请扫正确的二维码", Toast.LENGTH_SHORT).show();
                        //return;
                        scanBean=new ScanBean(barcodeStr,-1);
                    }
                    //showScanResult.setText(barcodeStr);
                    if (scanCallBack != null) {
                        scanCallBack.callBack(scanBean);
                    }
                }else{
                    //失败如超时等
                }

            }
        }

    };


    public ScanButView(Context context) {
        this(context,null);
    }

    public ScanButView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScanButView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOnLongClickListener(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(mBroadcastAction);
        getContext().registerReceiver(mScanReceiver, filter);
    }



    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isScaning = false;
        try {
            getContext().unregisterReceiver(mScanReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            stopScan();
        } else if (event.getAction() == MotionEvent.ACTION_DOWN){
            startScan();
        }
        return super.onTouchEvent(event);
    }

    private void stopScan() {
        Intent intent = new Intent();
        intent.setAction("nlscan.action.STOP_SCAN");
        getContext().sendBroadcast(intent);
    }

    @Override
    public boolean onLongClick(View v) {
        //startScan();
        return false;
    }

    private void startScan() {
        isScaning = true;
        Intent intent = new Intent();
        intent.setAction("nlscan.action.SCANNER_TRIG");
        getContext().sendBroadcast(intent);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void setScanCallBack(ScanCallBack scanCallBack) {
        this.scanCallBack = scanCallBack;
    }

    private ScanCallBack scanCallBack ;



    public interface ScanCallBack{
        void callBack(ScanBean scanBean);
    }
}
